<template>
  <el-card>
    <el-button size="small" type="primary" @click="handleAdd()">
      新增工具
    </el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="工具名称" />
      <el-table-column prop="description" label="工具描述" />
      <el-table-column prop="type" label="工具类型" width="120" />
      <el-table-column prop="free" label="是否免费" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.free === 1 ? 'success' : 'info'">
            {{ scope.row.free === 1 ? '免费' : '付费' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="100" />
      <el-table-column prop="usage_count" label="使用次数" width="100" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">
            {{ scope.row.status === 'active' ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleView(scope.row)">
            查看
          </el-button>
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="您确定要删除吗?" @confirm="handleRemove(scope.row.id)">
            <template #reference>
              <el-button size="small" type="warning">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查看对话框 -->
    <el-dialog v-model="viewDialogVisible" title="工具详情" width="50%">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="ID">{{ currentTool.id }}</el-descriptions-item>
        <el-descriptions-item label="工具名称">{{ currentTool.name }}</el-descriptions-item>
        <el-descriptions-item label="工具描述">{{ currentTool.description }}</el-descriptions-item>
        <el-descriptions-item label="工具类型">{{ currentTool.type }}</el-descriptions-item>
        <el-descriptions-item label="是否免费">{{ currentTool.free === 1 ? '免费' : '付费' }}</el-descriptions-item>
        <el-descriptions-item label="价格">{{ currentTool.price }}</el-descriptions-item>
        <el-descriptions-item label="使用次数">{{ currentTool.usage_count }}</el-descriptions-item>
        <el-descriptions-item label="缩略图">
          <img v-if="currentTool.thumbnail_url" :src="currentTool.thumbnail_url" style="max-width: 200px" />
          <span v-else>无图片</span>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentTool.created_at }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ currentTool.updated_at }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ currentTool.status === 'active' ? '启用' : '禁用' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" :title="editDialogTitle" width="50%">
      <el-form :model="currentTool" label-width="80px">
        <el-form-item label="ID">
          <el-input v-model="currentTool.id" disabled />
        </el-form-item>
        <el-form-item label="工具名称" required>
          <el-input v-model="currentTool.name" />
        </el-form-item>
        <el-form-item label="工具描述">
          <el-input v-model="currentTool.description" type="textarea" />
        </el-form-item>
        <el-form-item label="工具类型" required>
          <el-input v-model="currentTool.type" />
        </el-form-item>
        <el-form-item label="是否免费">
          <el-select v-model="currentTool.free" placeholder="请选择">
            <el-option label="免费" value="1" />
            <el-option label="付费" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="currentTool.price" type="number" />
        </el-form-item>
        <el-form-item label="缩略图URL">
          <el-input v-model="currentTool.thumbnail_url" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="currentTool.status" placeholder="请选择">
            <el-option label="启用" value="active" />
            <el-option label="禁用" value="inactive" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axiosObject from '@/main.js';

const tableData = ref([])
const viewDialogVisible = ref(false)
const editDialogTitle = ref("")
const editDialogVisible = ref(false)
const currentTool = ref({})

const handleView = (row) => {
  currentTool.value = { ...row }
  viewDialogVisible.value = true
}

const handleAdd = () => {
  editDialogTitle.value = "添加工具";
  currentTool.value = {
    free: 1,
    price: 0,
    usage_count: 0,
    status: 'active'
  };
  editDialogVisible.value = true;
}

const handleEdit = (row) => {
  editDialogTitle.value = "编辑工具";
  currentTool.value = { ...row }
  editDialogVisible.value = true
}

const handleRemove = async (toolId) => {
  try {
    await axiosObject.delete(`tool/${toolId}`);
    await findToolList();
  } catch (error) {
    console.error("删除失败:", error);
  }
}

const handleSave = async () => {
  try {
    if (currentTool.value.id) {
      // 修改工具
      await axiosObject.put(`tool/${currentTool.value.id}`, currentTool.value);
    } else {
      // 新增工具
      await axiosObject.post("tool", currentTool.value);
    }
    await findToolList();
    editDialogVisible.value = false
  } catch (error) {
    console.error("保存失败:", error);
  }
}

const findToolList = async () => {
  try {
    const result = await axiosObject.get("tool");
    console.log("工具数据:", result.data.resultData);
    tableData.value = result.data.resultData || [];
  } catch (error) {
    console.error("获取工具列表失败:", error);
    tableData.value = [];
  }
}

onMounted(() => {
  console.log("组件挂载完成，开始加载工具列表");
  findToolList();
});
</script>